import pymongo
from datetime import datetime
import sys
from datetime import timedelta
import calendar
import json
client = pymongo.MongoClient('mongodb://cs_user:cmcc5188@localhost:12018/cs_recharge')
db = client.cs_recharge
coll = db.rechange

year = sys.argv[1][:4]
month = sys.argv[1][4:]
_, month_range = calendar.monthrange(int(year), int(month))
orderdates = ["%s%s%02d" % (year, month, i+1) for i in range(month_range)]

for orderdate in orderdates:
    with open("%s_save.txt" % orderdate, "w") as f:
        # for orderdate in orderdates:
        test_range = coll.find({"merchantid": "M2018081416281534235296", "orderid": {"$ne": None}, "orderdate": orderdate})
        for i in test_range:
            i.pop("_id")
            f.write(json.dumps(i))
            f.write("\n")
